package com.cmwa.solution.sys.permission.post.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cmwa.solution.base.controller.BaseController;
import com.cmwa.solution.base.entity.Page;
import com.cmwa.solution.base.entity.Result;
import com.cmwa.solution.base.entity.SearchParam;
import com.cmwa.solution.exception.RepositoryException;
import com.cmwa.solution.sys.permission.post.entity.PostVo;
import com.cmwa.solution.sys.permission.post.service.PostService;

@Controller
@RequestMapping(value = "/sys/oper/post")
public class PostController extends BaseController{
	
	@Resource
	private PostService postService;
	
	/**
	 * 岗位列表页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/listView")
	public String listView() {
		return "system/sys/permission/post/postList";
	}
	
	/**
	 * 列表页面数据
	 * @param sp
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/postListPage")
	public Page<PostVo> postListPage(SearchParam sp) {
		Page<PostVo> list = postService.postListPage(sp);
		return list;
	}
	
	/**
	 * 新增岗位页面
	 * @return
	 */
	@RequestMapping("/postAddView")
	public String postAddView() {
		return "system/sys/permission/post/postAdd";
	}
	
	/**
	 * 修改岗位页面
	 * @param postId
	 * @param model
	 * @return
	 */
	@RequestMapping("/postUpdateView")
	public String postUpdateView(@RequestParam("postId") String postId,ModelMap model) {
		PostVo postVo = postService.detailById(postId);
		model.addAttribute("postVo", postVo);
		return "system/sys/permission/post/postUpdate";
	}
	
	/**
	 * 岗位详情页面
	 * @param model
	 * @return
	 */
	@RequestMapping("/detailView")
	public String detailView(@RequestParam("postId") String postId,ModelMap model) {
		PostVo postVo = postService.detailById(postId);
		model.addAttribute("postVo", postVo);
		return "system/sys/permission/post/postDetail";
	}
	
	/**
	 * 新增岗位
	 * @param postVo
	 * @return
	 * @throws RepositoryException
	 */
	@RequestMapping("/savePost")
	public String savePost(PostVo postVo) throws RepositoryException{
		postService.savePost(postVo);
		return redirectSuccess();
	}
	
	/**
	 * 更新岗位
	 * @param postVo
	 * @return
	 * @throws RepositoryException
	 */
	@RequestMapping("/updatePost")
	public String updatePost(PostVo postVo) throws RepositoryException{
		postService.updatePost(postVo);
		return redirectSuccess();
	}
	
	/**
	 * 检查是否重复
	 * @param postTitle
	 * @param postCode
	 * @param postId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/checkRepeatPost")
	public Result<Object> checkRepeatPost(@RequestParam(value="postTitle",required=false) String postTitle ,@RequestParam(value="postCode",required=false) String postCode ,@RequestParam(value="postId",required=false) String postId) throws Exception {
		Result<Object> result = new Result<Object>();
		boolean isRepeat = postService.checkRepeatPost(postTitle,postCode,postId);
		result.setObject(isRepeat);
		return result;
	}
	
	/**
	 * 删除岗位
	 * @param postId
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/deletePost")
	public Result<Object> deletePost(@RequestParam("postId") String postId) throws RepositoryException {
		Result<Object> result = new Result<Object>();
		result.setMessage("删除成功!");
		result.setSuccess(true);
		try {
			postService.deletePostWithStatus(postId);
		} catch (Exception e) {
			result.setMessage("删除失败!");
			result.setSuccess(false);
		}
		return result;
	}

}




